<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------

namespace app\api\logic;

use app\common\logic\LogicBase;
use \tpfcore\Core;
use think\Validate;

class AliyunSdk extends ApiBase
{
    public function getExpress($ship_number){

        if(file_exists("ship/{$ship_number}.txt")){

            $restult = file_get_contents("ship/{$ship_number}.txt");

            $result = json_decode($restult);


        }else{
            $host = "https://kuaidid.market.alicloudapi.com";
            $path = "/danhao";
            $method = "POST";
            $appcode = "d4b9c8549b934d39ba74933637e9e76a";
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);
            //根据API的要求，定义相对应的Content-Type
            array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
            $querys = "";
            $bodys = "src=543504132797";
            $url = $host . $path;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            if (1 == strpos("$".$host, "https://"))
            {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

            $res = curl_exec($curl);

            $result = json_decode($res);

            if($result->msg->state==3){  //表示已经完成，记录下来作为缓存，不要每次都去查询
                file_put_contents("ship/{$ship_number}.txt", $res);
            }

        }

        $context = $result->msg;

        $content = $context->context;

        //print_r($context);die;

        foreach ($content as $key => $value) {
            $value->time=date('Y-m-d H:i:s',$value->time);
            $content[$key]=$value;
        }

        $context->context=$content;

        
        return $context;
    }
}